﻿using System;
using System.Web;

namespace GiveAQuiz.Web.Helpers
{
  /// <summary>
  /// This class implements the methods defined in the
  /// IWebPathHelper interface by wrapping calls to 
  /// static and utility methods found in the ASP.Net
  /// library.
  /// </summary>
  public class WebPathHelper : IWebPathHelper
  {
    /// <summary>
    /// Given a virtual path to a resource, create the full URL
    /// path which incorporates the host name and Application 
    /// virtual root.
    /// </summary>
    /// <param name="virtualPath">The virtual path (e.g. ~/Account/LogOn)</param>
    /// <returns>The full URL path (e.g. 
    /// http://www.give-a-quiz.com/GiveAQuiz/Account/Logon)</returns>
    public string CreateFullUrl(string virtualPath)
    {
      HttpRequestWrapper request = new HttpRequestWrapper(HttpContext.Current.Request);
      var baseUrl = request.HostUrl();
      return String.Format("{0}{1}", baseUrl, VirtualPathUtility.ToAbsolute(virtualPath));
    }

    /// <summary>
    /// Given a relative path to a resource, create the path 
    /// which incorporates the Application virtual root.
    /// </summary>
    /// <param name="virtualPath">The virtual path (e.g. ~/Account/LogOn)</param>
    /// <returns>The application path (e.g. /GiveAQuiz/Account/Logon)</returns>
    public string CreateApplicationUrl(string virtualPath)
    {
      return VirtualPathUtility.ToAbsolute(virtualPath);
    }

    /// <summary>
    /// Given a virtual path, return the actual physical
    /// path of the resource on the file system.
    /// </summary>
    /// <param name="virtualPath">The virtual path (e.g. ~/App_Data/foo.txt)</param>
    /// <returns>The physical path (e.g. C:\Inetpub\wwwroot\GiveAQuiz\App_Data\foo.txt)</returns>
    public string MapPhysicalPath(string virtualPath)
    {
      return HttpContext.Current.Server.MapPath(virtualPath);
    }
  }
}
